{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# numpy 库\n",
    "#\n",
    "import numpy as np\n",
    "\n",
    "# 创建数组\n",
    "# object\t数组或嵌套的数列\n",
    "# dtype\t数组元素的数据类型，可选\n",
    "# copy\t对象是否需要复制，可选\n",
    "# order\t创建数组的样式，C为行方向，F为列方向，A为任意方向（默认）\n",
    "# subok\t默认返回一个与基类类型一致的数组\n",
    "# ndmin\t指定生成数组的最小维度\n",
    "\n",
    "# numpy.array(object, dtype=None, copy=True, order=None, subok=False, ndmin=0)\n",
    "\n",
    "\n",
    "def demo():\n",
    "    a = np.array([1, 2, 3])\n",
    "    print(a)\n",
    "\n",
    "    # 多于一个维度\n",
    "    a2 = np.array([[1,  2],  [3,  4]])\n",
    "    print(a2)\n",
    "\n",
    "    # dtype 参数\n",
    "    a3 = np.array([1,  2,  3], dtype=complex)\n",
    "    print(a3)\n",
    "    return\n",
    "\n",
    "\n",
    "# NumPy 数组属性\n",
    "def numpy_array_attrib():\n",
    "    \"\"\"\n",
    "    ndarray.ndim\t    数组对象的轴或维度的数量(秩)\n",
    "    ndarray.shape\t    数组的维度, 对于矩阵, n 行 m 列\n",
    "    ndarray.size\t    数组元素的总个数, 相当于 ndarray.shape 中 n*m 的值\n",
    "    ndarray.dtype\t    数组对象的元素类型\n",
    "    ndarray.itemsize    数组对象中每个元素的大小，以字节为单位\n",
    "    ndarray.flags\t    数组对象的内存信息\n",
    "    ndarray.real\t    数组元素的实部\n",
    "    ndarray.imag\t    数组元素的虚部\n",
    "    ndarray.data\t    数组元素的缓冲区，由于一般通过数组的索引获取元素，所以通常不需要使用这个属性。\n",
    "    \"\"\"\n",
    "\n",
    "    # [1] ndarray.ndim\n",
    "    print('[1] ndarray.ndim - 轴的数量或维度的数量(秩)')\n",
    "    a = np.arange(24)       #\n",
    "    print(a.ndim)           # a 现只有一个维度\n",
    "    b = a.reshape(2, 4, 3)  # 现在调整其大小\n",
    "    print(b.ndim)           # b 现在拥有三个维度\n",
    "    print()\n",
    "\n",
    "    # [2] ndarray.shape\n",
    "    print('[2] ndarray.shape - 数组的维度, 对于矩阵表示 n 行 m 列')\n",
    "    # a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "    a = np.array([1, 2, 3, 4, 5, 6], order='C')\n",
    "    print(\"a.shape = \", a.shape)  # (6,1)\n",
    "    print(a)\n",
    "    print()\n",
    "\n",
    "    a.shape = (2, 3)\n",
    "    print(\"a.shape = \", a.shape)\n",
    "    print(a)\n",
    "    print()\n",
    "\n",
    "    a.shape = (3, 2)\n",
    "    print(\"a.shape = \", a.shape)\n",
    "    print(a)\n",
    "    print()\n",
    "    # b = a.reshape(2, 3)\n",
    "\n",
    "    # [3] ndarray.size\t    数组元素的总个数，相当于 .shape 中 n*m 的值\n",
    "    print(\"[3] ndarray.size - 数组元素的总个数, 相当于ndarray.shape 中 n*m 的值\")\n",
    "    a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "    print(\"a.shape = \", a.shape)\n",
    "    print(\"a.size = \", a.size)\n",
    "    print()\n",
    "\n",
    "    # [4] ndarray.dtype\n",
    "    print(\"[4] ndarray.dtype - ndarray 对象的元素类型\")\n",
    "    x = np.array([1, 2, 3, 4, 5])\n",
    "    print(\"ndarray.dtype: \", x.dtype)\n",
    "\n",
    "    # [5] ndarray.itemsize\n",
    "    print(\"[5] ndarray.itemsize - 数组元素的总个数, 相当于ndarray.shape 中 n*m 的值\")\n",
    "    x = np.array([1, 2, 3, 4, 5], dtype=np.int8)\n",
    "    print(\"x.itemsize: \", x.itemsize)\n",
    "    # 数组的 dtype 现在为 float64（八个字节）\n",
    "    # y = np.array([1, 2, 3, 4, 5], dtype=np.float64)\n",
    "    # print(y.itemsize)\n",
    "    # print()\n",
    "\n",
    "    # [6] ndarray.flags\n",
    "    print(\"[6] ndarray.flags - 数组对象的内存信息\")\n",
    "    a = np.array([[1, 2, 3], [4, 5, 6]])\n",
    "    print(\"ndarray.flags: \", a.flags)\n",
    "    print()\n",
    "    # [7] ndarray.real\n",
    "    print(\"[7] ndarray.real - \")\n",
    "    # [8] ndarray.imag\n",
    "    print(\"[8] ndarray.imag - \")\n",
    "    # [9] ndarray.data\n",
    "    print(\"[9] ndarray.data - \")\n",
    "\n",
    "    return\n",
    "\n",
    "\n",
    "# demo()\n",
    "numpy_array_attrib()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.9 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "cf18841ace8313d0bc088ca146c17a6c0040e82121d5cb75c0ea07172309253d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
